Skip to content

Method: greaterThan(Expression, Comparable)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.query.criteria;
14:
15: import cz.cvut.kbss.jopa.model.CriteriaQueryImpl;
16: import cz.cvut.kbss.jopa.model.query.criteria.*;
17: import cz.cvut.kbss.jopa.query.criteria.expressions.*;
18: import cz.cvut.kbss.jopa.sessions.CriteriaBuilder;
19: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
20:
21: import java.util.Arrays;
22:
23: public class CriteriaBuilderImpl implements CriteriaBuilder {
24:
25: private final UnitOfWorkImpl uow;
26:
27: public CriteriaBuilderImpl(UnitOfWorkImpl uow) {
28: this.uow = uow;
29: }
30:
31: @Override
32: public <T> CriteriaQuery<T> createQuery(Class<T> resultClass) {
33: return new CriteriaQueryImpl<>(new CriteriaQueryHolder<>(resultClass), uow.getMetamodel(), this);
34: }
35:
36: @Override
37: public <N extends Number> Expression<N> abs(Expression<N> x) {
38: validateFunctionArgument(x);
39: return new AbsFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
40: }
41:
42: @Override
43: public Expression<Integer> count(Expression<?> x) {
44: validateFunctionArgument(x);
45: return new CountFunction((AbstractPathExpression) x, this);
46: }
47:
48: @Override
49: public <N extends Number> Expression<N> ceil(Expression<N> x) {
50: validateFunctionArgument(x);
51: return new CeilFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
52: }
53:
54: @Override
55: public <N extends Number> Expression<N> floor(Expression<N> x) {
56: validateFunctionArgument(x);
57: return new FloorFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
58: }
59:
60: @Override
61: public Expression<Integer> length(Expression<String> x) {
62: validateFunctionArgument(x);
63: return new LengthFunction((AbstractPathExpression) x, this);
64: }
65:
66: @Override
67: public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
68: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
69: return new ParameterExpressionImpl<>(paramClass, null, this);
70: }
71:
72: @Override
73: public <T> ParameterExpression<T> parameter(Class<T> paramClass, String name) {
74: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
75: return new ParameterExpressionImpl<>(paramClass, name, this);
76: }
77:
78: @Override
79: public <T> Expression<T> literal(T value) {
80: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
81: return new ExpressionLiteralImpl<>(value, this);
82: }
83:
84: @Override
85: public Expression<String> literal(String value, String languageTag) {
86: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
87: return new ExpressionLiteralImpl<>(value, languageTag, this);
88: }
89:
90: @Override
91: public Expression<String> lower(Expression<String> x) {
92: validateFunctionArgument(x);
93: return new LowerFunction((AbstractPathExpression) x, this);
94: }
95:
96: @Override
97: public Expression<String> upper(Expression<String> x) {
98: validateFunctionArgument(x);
99: return new UpperFunction((AbstractPathExpression) x, this);
100: }
101:
102: private void validateFunctionArgument(Expression<?> x) {
103: if (!(x instanceof AbstractPathExpression)) {
104: throw new IllegalArgumentException("Function can be applied only to path expressions.");
105: }
106: }
107:
108: @Override
109: public Order asc(Expression<?> x) {
110: return new OrderImpl(x);
111: }
112:
113: @Override
114: public Order desc(Expression<?> x) {
115: return new OrderImpl(x, false);
116: }
117:
118:
119: @Override
120: public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
121: return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(x, y), this);
122: }
123:
124: @Override
125: public Predicate and(Predicate... restrictions) {
126: if (restrictions.length == 1) return new SimplePredicateImpl(restrictions[0], this);
127: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(restrictions), this);
128: }
129:
130: @Override
131: public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
132: return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(x, y), this);
133: }
134:
135: @Override
136: public Predicate or(Predicate... restrictions) {
137: if (restrictions.length == 1)
138: return new SimplePredicateImpl(Predicate.BooleanOperator.OR, restrictions[0], this);
139: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(restrictions), this);
140: }
141:
142: @Override
143: public Predicate equal(Expression<?> x, Expression<?> y) {
144: return new SimplePredicateImpl(
145: new ExpressionEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
146: }
147:
148: @Override
149: public Predicate equal(Expression<?> x, Object y) {
150: return new SimplePredicateImpl(
151: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this), this);
152: }
153:
154: @Override
155: public Predicate equal(Expression<?> x, String y, String languageTag) {
156: return new SimplePredicateImpl(
157: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, languageTag, this),
158: this), this);
159: }
160:
161: @Override
162: public Predicate notEqual(Expression<?> x, Expression<?> y) {
163: return new SimplePredicateImpl(
164: new ExpressionNotEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
165: }
166:
167: @Override
168: public Predicate notEqual(Expression<?> x, Object y) {
169: return new SimplePredicateImpl(
170: new ExpressionNotEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this),
171: this);
172:
173: }
174:
175: @Override
176: public Predicate like(Expression<String> x, Expression<String> pattern) {
177: return new SimplePredicateImpl(
178: new ExpressionLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
179: this);
180: }
181:
182: @Override
183: public Predicate like(Expression<String> x, String pattern) {
184: return new SimplePredicateImpl(
185: new ExpressionLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
186: this), this);
187: }
188:
189: @Override
190: public Predicate notLike(Expression<String> x, Expression<String> pattern) {
191: return new SimplePredicateImpl(
192: new ExpressionNotLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
193: this);
194: }
195:
196: @Override
197: public Predicate notLike(Expression<String> x, String pattern) {
198: return new SimplePredicateImpl(
199: new ExpressionNotLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
200: this), this);
201: }
202:
203: @Override
204: public Predicate not(Expression<Boolean> restriction) {
205: return wrapExpressionToPredicateWithRepair(restriction).not();
206: }
207:
208: @Override
209: public <T> In<T> in(Expression<? extends T> expression) {
210: return new ExpressionInImpl<>(expression, this);
211: }
212:
213: @Override
214: public <T> In<T> notIn(Expression<? extends T> expression) {
215: In<T> inExpression = new ExpressionInImpl<>(expression, this);
216: inExpression.not();
217: return inExpression;
218: }
219:
220: @Override
221: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x,
222: Expression<? extends Y> y) {
223: return new SimplePredicateImpl(
224: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
225: }
226:
227: @Override
228: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Y y) {
229: return new SimplePredicateImpl(
230: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
231: this);
232: }
233:
234: @Override
235: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x,
236: Expression<? extends Y> y) {
237: return new SimplePredicateImpl(
238: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
239: }
240:
241: @Override
242: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x, Y y) {
243: return new SimplePredicateImpl(
244: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
245: this), this);
246: }
247:
248: @Override
249: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Expression<? extends Y> y) {
250: return new SimplePredicateImpl(
251: new ExpressionLessThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
252: }
253:
254: @Override
255: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y) {
256: return new SimplePredicateImpl(
257: new ExpressionLessThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
258: this);
259: }
260:
261: @Override
262: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x,
263: Expression<? extends Y> y) {
264: return new SimplePredicateImpl(
265: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
266: }
267:
268: @Override
269: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x, Y y) {
270: return new SimplePredicateImpl(
271: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
272: this), this);
273: }
274:
275:
276: /**
277: * Method wraps given boolean expression to Predicate and if path expression occur, it wrap it to
278: * ExpressionEqualsImpl before. For example: {@literal Expression<Boolean> expression =
279: * factory.get("attributeName");} Looks like boolean expression but in fact it is not boolean expression, so we need
280: * to fix this.
281: *
282: * @param expression - boolean or path expression
283: * @return Expression wrapped in Predicate
284: */
285: public Predicate wrapExpressionToPredicateWithRepair(Expression<Boolean> expression) {
286: if (expression instanceof Predicate) {
287: return (Predicate) expression;
288: } else if (expression instanceof AbstractPathExpression) {
289: return new SimplePredicateImpl(
290: new ExpressionEqualImpl((AbstractExpression) expression, (AbstractExpression) this.literal(true),
291: this), this);
292: } else {
293: return new SimplePredicateImpl(expression, this);
294: }
295: }
296:
297: }